
#include <assert.h>
#include "app_platform/cmd_obj.h"
#include "app_platform/cntl_info.h"
#include "app_platform/intf_service_control.h"
#include "giserpdao_adaptor.h"
#include "giserpdao_req_resp.h"


std::string g_sgiserpdaoStandAlone;
#include "app_platform/res_factory.h"
#include "c2cplatform/component/dal/dal_set.h"

using namespace c2cplatform::component::dal;

///////////////////////
// class CGisErpDaoSvcFactory
int CGisErpDaoSvcFactory::init(void* pvParam)
{
	IUserDefineCntl* pCntl = reinterpret_cast<IUserDefineCntl*>(pvParam);
	assert(pCntl);
		CDAL_SET->AddCmd("0xc0241001");
		CDAL_SET->AddCmd("0xc0241002");

	return CGisErpDao::Initialize(pCntl);
}

int CGisErpDaoSvcFactory::CXYZTimerHandler::OnTimer(int iTimerId)
{
    assert(0);
	return 0;
}

int CGisErpDaoSvcFactory::fini(void* pvParam)
{
    return 0;
}

uint32_t CGisErpDaoSvcFactory::getCateId() const
{   
	//方法已废弃
    return 0;
}

IService* CGisErpDaoSvcFactory::createService(uint32_t dwcmdId)
{
    return (new CGisErpDaoService(dwcmdId));
}

void CGisErpDaoSvcFactory::getServiceType(ServiceTypeMap_T& mapSvcType)
{
	mapSvcType[GISERPDAO_GETMAPCOVERLOCUSLIST_REQ_CMD_ID] = true;
	mapSvcType[GISERPDAO_SHIPTYPEAREALOCUSROWSYNC_REQ_CMD_ID] = true;
}

////////////////////////
// class CGisErpDaoService
CGisErpDaoService::CGisErpDaoService(uint32_t dwcmdId) 
: m_dwcmdId(dwcmdId)
{
    m_pSvc = new CGisErpDao;
}

void CGisErpDaoService::Reset() 
{ 
	m_pSvc -> Reset();
}
    
void CGisErpDaoService::getCmdIdRange(uint32_t& dwcmdIdBegin, uint32_t& dwcmdIdEnd) const 
{ 
    dwcmdIdBegin = m_dwcmdId;
    dwcmdIdEnd = m_dwcmdId;
} 
	
int CGisErpDaoService::Execute(CCmdObj& oCmd)
{
	switch(oCmd.getCmdId())
	{
	case GISERPDAO_GETMAPCOVERLOCUSLIST_REQ_CMD_ID:
        return GetMapCoverLocusList(oCmd);
	case GISERPDAO_SHIPTYPEAREALOCUSROWSYNC_REQ_CMD_ID:
        return ShipTypeAreaLocusRowSync(oCmd);
	default:
		assert(0);
	}
	return 0;
}

int CGisErpDaoService::GetMapCoverLocusList(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisErpDaoGetMapCoverLocusListReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	CGisErpDaoGetMapCoverLocusListResp oResp;
	
    oResp.dwResult = m_pSvc->GetMapCoverLocusList(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.shipType,
		oReq.destGbId,
		oReq.inReserve,
		oReq.shipTypeAreaLocusDoList,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.shipTypeAreaLocusDoList = oReq.shipTypeAreaLocusDoList;
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CGisErpDaoService::ShipTypeAreaLocusRowSync(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisErpDaoShipTypeAreaLocusRowSyncReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	CGisErpDaoShipTypeAreaLocusRowSyncResp oResp;
	
    oResp.dwResult = m_pSvc->ShipTypeAreaLocusRowSync(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.shipTypeAreaLocusDo,
		oReq.inReserve,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


#include "svc_main.h"

IServiceFactory* GetServiceFactory(void)
{
    return CSvcFactorySingleton::Instance();
}
